﻿using DAL;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Drawing;

namespace ProductShop
{
    class PresenterMain : IPresenterMain
    {
        private readonly IProductShopView view;
        private readonly IUnitOfWork unitOfWork;
        public PresenterMain(IProductShopView view, IUnitOfWork unitOfWork)
        {
            this.view = view;
            this.unitOfWork = unitOfWork;

            this.view.ProductCategoryChanged += OnCategoryChanged;
            this.view.ParticularProductSelected += OnProductSelected;
            this.view.ProductDeleted += OnProductDeleted;
        }

        private void OnProductDeleted()
        {
            Int32 selectedProduct = Int32.Parse(view.GetSelectedProductID());
            Product delete = unitOfWork.Products.Get(selectedProduct);
            unitOfWork.Products.Remove(delete);
            unitOfWork.Commit();
        }

        private void OnProductSelected()
        {
            Int32 selectedProductID = 0;
            if (Int32.TryParse(view.GetSelectedProductID(), out selectedProductID))
            {
                Product selected = unitOfWork.Products.Get(selectedProductID);
                view.SelectedProductID = selected.ProductCategoryId.ToString();
                view.SelectedProductName = selected.Name;
                view.SelectedProductPrice = selected.Price.ToString();
                view.SelectedProductDescription = selected.Description;
                view.SelectedProductImage = LoadImageFromWeb(selected.ImageURL);
            }
        }

        private void OnCategoryChanged()
        {
            String selectedProductCategory = view.GetSelectedProductCategory();
            view.ProductData = LoadProductsByCategory(selectedProductCategory);
        }

        private IEnumerable<String> GetCategories()
        {
            List<String> productCategories = new List<String>();
            foreach (var category in unitOfWork.ProductCategories.GetAll())
            {
                productCategories.Add(category.Name);
            }
            return productCategories;
        }

        private IEnumerable<Product> LoadProductsByCategory(string category)
        {
            IEnumerable<Product> products =
                unitOfWork.Products.Find(product => product.ProductCategory.Name == category).ToArray();
            return products;
        }

        private Image LoadImageFromWeb(string url)
        {
            HttpWebRequest webRequest = HttpWebRequest.Create(url) as HttpWebRequest;
            HttpWebResponse webresponse = webRequest.GetResponse() as HttpWebResponse;
            using (Stream stream = webresponse.GetResponseStream())
            {
                return Image.FromStream(stream);
            }
        }
        public void ShowMainView()
        {
            view.ProductCategoryNames = GetCategories();
            view.ShowForm();
        }
        public void CloseMainView()
        {
            view.CloseForm();
        }
    }
}
